package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Customer;
import com.sl.au.entity.CustomerVisitPerson;
import com.sl.au.entity.CustomerVisiting;
import com.sl.au.entity.CustomerVisitingCost;
import com.sl.au.entity.Module;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.repository.CustomerVisitPersonRepository;
import com.sl.au.repository.CustomerVisitingCostRepository;
import com.sl.au.service.CustomerService;
import com.sl.au.service.CustomerVisitingService;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.SaleAreaService;

@RestController
@RequestMapping(value = "/ui/customerVisiting")
public class CustomerVisitingController extends BaseController {

	@Autowired
	CustomerVisitingService customerVisitingService;

	@Autowired
	SaleAreaService saleAreaService;

	@Autowired
	OperateLogService operateLogService;

	@Autowired
	CustomerService customerService;
	@Autowired
	CustomerVisitPersonRepository customerVisitPersonRepository;

	@Autowired
	CustomerVisitingCostRepository customerVisitingCostRepository;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("customerVisiting/customerVisitingIndex");
		String name = "customervisit";
		Module module = this.getPageModule(name);
		modelAndView.addObject("module", module);
		return modelAndView;
	}

	//// 打开新增页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addVisit(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("customerVisiting/customerVisitingAdd");
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		mv.addObject("saleAreas", saleAreaService.getRepository().findBySuperior(superior));
		mv.addObject("visitDate", new Date());
		mv.addObject("customers", customerService.getRepository().findCustomerBySuperior(superior));
		return mv;
	}

	///// 打开编辑页面
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView editVisit(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("customerVisiting/customerVisitingEdit");
		String visitId = request.getParameter("visitId");
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		if (visitId != null) {
			CustomerVisiting customerVisiting = customerVisitingService.getCustomerVisitingRepository()
					.findById(visitId);
			mv.addObject("visit", customerVisiting);
		}
		mv.addObject("saleAreas", saleAreaService.getRepository().findBySuperior(superior));
		mv.addObject("customers", customerService.getRepository().findCustomerBySuperior(superior));
		return mv;
	}

	///// 查看客户来访
	@RequestMapping(value = "see", method = RequestMethod.GET)
	public ModelAndView seeVisit(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("customerVisiting/customerVisitingSee");
		String visitId = request.getParameter("visitId");
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		if (visitId != null) {
			CustomerVisiting customerVisiting = customerVisitingService.getCustomerVisitingRepository()
					.findById(visitId);
			mv.addObject("visit", customerVisiting);
		}
		mv.addObject("saleAreas", saleAreaService.getRepository().findBySuperior(superior));
		mv.addObject("customers", customerService.getRepository().findCustomerBySuperior(superior));
		return mv;
	}

	//// 获得客户来访信息列表
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String name = "customervisit";
		Module module = this.getPageModule(name);
		String dataFilter = module.getDataFilter();
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		String areaName = a.getUser().getSaleArea().getAreaName();
		List<CustomerVisiting> visitings = new ArrayList<CustomerVisiting>();
		visitings = customerVisitingService.getVisit(start, length, key, dataFilter, superior, areaName, userId);
		long count = customerVisitingService.getVisitCount(key, dataFilter, superior, areaName, userId);

		map.put("data", visitings);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 获得一个客户的来访信息列表
	@RequestMapping(value = "getvisitpage", method = RequestMethod.GET)
	public Map<String, Object> getVisitPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String customerId = request.getParameter("customerId");
		List<CustomerVisiting> visitings = new ArrayList<CustomerVisiting>();
		visitings = customerVisitingService.getVisitById(start, length, key, customerId);
		long count = 0;
		count = customerVisitingService.getCustomerVisitingRepository().getCustomerCount(customerId, key);
		map.put("data", visitings);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 保存客户来访
	@RequestMapping(value = "saveCustomerVisit", method = RequestMethod.POST)
	public Map<String, Object> saveCustomerVisit(HttpServletRequest request,
			@RequestBody CustomerVisiting customerVisiting) {
		Map<String, Object> map = new HashMap<String, Object>();
		String customerId = customerVisiting.getCustomer().getId();
		Customer c = customerService.getRepository().findById(customerId);
		customerVisiting.setCustomer(c);
		Account a = this.getMyAccount();
		User user = a.getUser();
		customerVisiting.setUser(user);
		CustomerVisiting old = customerVisitingService.getCustomerVisitingRepository()
				.findOne(customerVisiting.getId());
		if (old != null) {
			String content = "编辑客户来访【" + customerVisiting.getCustomer().getCustomerName() + "】";
			operateLogService.log(user, "编辑客户来访", content);
		} else {
			String content = "新增客户【" + customerVisiting.getCustomer().getCustomerName() + "】";
			operateLogService.log(user, "新增投标", content);
		}
		customerVisitingService.getCustomerVisitingRepository().save(customerVisiting);
		map.put("code", 0);
		String visitId = customerVisiting.getId();
		Date createTime = customerVisiting.getCreateTime();
		map.put("createTime", createTime);
		map.put("visitId", visitId);
		return map;
	}

	/// 获得来访人员列表
	@RequestMapping(value = "getVisitPerson", method = RequestMethod.GET)
	public Map<String, Object> getVisitPerson(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String visitId = request.getParameter("visitId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<CustomerVisitPerson> visitPersons = new ArrayList<CustomerVisitPerson>();
		if (StringUtils.hasText(visitId)) {
			visitPersons = customerVisitingService.getVisitPerson(start, length, key, visitId);
		}
		long count = 0;
		if (key != null) {
			count = customerVisitPersonRepository.getCount(visitId, key);
		} else {
			count = customerVisitPersonRepository.getCount(visitId);
		}
		map.put("data", visitPersons);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 保存来访人员信息
	@RequestMapping(value = "saveVisitPerson", method = RequestMethod.POST)
	public Map<String, Object> saveKeyContact(HttpServletRequest request,
			@RequestBody CustomerVisitPerson visitPerson) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = visitPerson.getCustomerVisiting().getId();
		CustomerVisiting v = customerVisitingService.getCustomerVisitingRepository().findById(id);
		visitPerson.setCustomerVisiting(v);
		customerVisitingService.getCustomerVisitPersonRepository().save(visitPerson);
		map.put("code", 0);
		return map;
	}

	// 删除来访人员信息
	@RequestMapping(value = "delVisitPerson", method = RequestMethod.GET)
	public Map<String, Object> delVisitPerson(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<CustomerVisitPerson> persons = new ArrayList<CustomerVisitPerson>();
		for (int i = 0; i < id.length; i++) {
			String visitId = id[i];
			CustomerVisitPerson c = new CustomerVisitPerson();
			c.setId(visitId);
			persons.add(c);
		}
		customerVisitingService.getCustomerVisitPersonRepository().deleteInBatch(persons);
		map.put("code", 0);
		return map;
	}

	//// 获得来访的费用
	@RequestMapping(value = "getVisitCost", method = RequestMethod.GET)
	public Map<String, Object> getVisitCost(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String visitId = request.getParameter("visitId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<CustomerVisitingCost> visitPersons = new ArrayList<CustomerVisitingCost>();
		if (StringUtils.hasText(visitId)) {
			visitPersons = customerVisitingService.getVisitingCost(start, length, key, visitId);
		}
		Long count = (long) 0;
		if (key != null) {
			count = customerVisitingService.getCustomerVisitingCostRepository().getCount(visitId, key);
		} else {
			count = customerVisitingService.getCustomerVisitingCostRepository().getCount(visitId);
		}
		map.put("data", visitPersons);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 保存来访费用
	@RequestMapping(value = "saveVisitCost", method = RequestMethod.POST)
	public Map<String, Object> saveVisitCost(HttpServletRequest request, @RequestBody CustomerVisitingCost visitCost) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = visitCost.getCustomerVisiting().getId();
		CustomerVisiting v = customerVisitingService.getCustomerVisitingRepository().findById(id);
		visitCost.setCustomerVisiting(v);
		customerVisitingService.getCustomerVisitingCostRepository().save(visitCost);
		map.put("code", 0);
		return map;
	}

	// 删除来访费用
	@RequestMapping(value = "delVisitCost", method = RequestMethod.GET)
	public Map<String, Object> delVisitCost(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<CustomerVisitingCost> costs = new ArrayList<CustomerVisitingCost>();
		for (int i = 0; i < id.length; i++) {
			String costId = id[i];
			CustomerVisitingCost c = new CustomerVisitingCost();
			c.setId(costId);
			costs.add(c);
		}
		customerVisitingService.getCustomerVisitingCostRepository().deleteInBatch(costs);
		map.put("code", 0);
		return map;
	}

	///// 删除来访信息
	@RequestMapping(value = "deleteVisit", method = RequestMethod.GET)
	public Map<String, Object> deleteVisit(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		Account a = this.getMyAccount();
		User user = a.getUser();
		for (int i = 0; i < id.length; i++) {
			String visitId = id[i];
			customerVisitingService.getCustomerVisitingCostRepository().deleteCost(visitId);
			customerVisitingService.getCustomerVisitPersonRepository().deletePerson(visitId);
			CustomerVisiting old=customerVisitingService.getCustomerVisitingRepository().findOne(visitId);
			String content  = "删除客户来访【" + old.getCustomer().getCustomerName() + "】";
			operateLogService.log(user, "删除客户", content);
			customerVisitingService.getCustomerVisitingRepository().delete(visitId);
			
		}
		map.put("code", 0);
		return map;
	}

}
